home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / SCHEME / GNU / SCM4E1 / !Scm / slib / comlist < prev    next >
Text File  |  1993-09-08  |  7KB  |  243 lines

  1. ;;;; Implementation of COMMON LISP list functions for Scheme
  2. ;;; Copyright (C) 1991, 1993 Aubrey Jaffer.
  3.  
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7.  
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10.  
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14.  
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. ;;; Some of these functions may be already defined in your Scheme.
  21. ;;; Comment out those definitions for functions which are already defined.
  22.  
  23. ;;;; LIST FUNCTIONS FROM COMMON LISP
  24.  
  25. ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
  26. (define (comlist:make-list k . init)
  27.   (set! init (if (pair? init) (car init)))
  28.   (do ((k k (+ -1 k))
  29.        (result '() (cons init result)))
  30.       ((<= k 0) result)))
  31.  
  32. (define (comlist:copy-list lst) (append lst '()))
  33.  
  34. (define (comlist:adjoin e l) (if (memq e l) l (cons e l)))
  35.  
  36. (define (comlist:union l1 l2)
  37.   (cond ((null? l1) l2)
  38.     ((null? l2) l1)
  39.     (else (comlist:union (cdr l1) (comlist:adjoin (car l1) l2)))))
  40.  
  41. (define (comlist:intersection l1 l2)
  42.   (cond ((null? l1) l1)
  43.     ((null? l2) l2)
  44.     ((memv (car l1) l2) (cons (car l1) (comlist:intersection (cdr l1) l2)))
  45.     (else (comlist:intersection (cdr l1) l2))))
  46.  
  47. (define (comlist:set-difference l1 l2)
  48.   (cond ((null? l1) l1)
  49.     ((memv (car l1) l2) (comlist:set-difference (cdr l1) l2))
  50.     (else (cons (car l1) (comlist:set-difference (cdr l1) l2)))))
  51.  
  52. (define (comlist:position obj lst)
  53.   (letrec ((pos (lambda (n lst)
  54.           (cond ((null? lst) #f)
  55.             ((eqv? obj (car lst)) n)
  56.             (else (pos (+ 1 n) (cdr lst)))))))
  57.     (pos 0 lst)))
  58.  
  59. (define (comlist:reduce-init p init l)
  60.   (if (null? l)
  61.       init
  62.       (comlist:reduce-init p (p init (car l)) (cdr l))))
  63.  
  64. (define (comlist:reduce p l)
  65.   (cond ((null? l) l)
  66.     ((null? (cdr l)) (car l))
  67.     (else (comlist:reduce-init p (car l) (cdr l)))))
  68.  
  69. (define (comlist:some pred l . rest)
  70.   (cond ((null? rest)
  71.      (let mapf ((l l))
  72.        (and (not (null? l))
  73.         (or (pred (car l)) (mapf (cdr l))))))
  74.     (else (let mapf ((l l) (rest rest))
  75.         (and (not (null? l))
  76.              (or (apply pred (car l) (map car rest))
  77.              (mapf (cdr l) (map cdr rest))))))))
  78.  
  79. (define (comlist:every pred l . rest)
  80.   (cond ((null? rest)
  81.      (let mapf ((l l))
  82.        (or (null? l)
  83.            (and (pred (car l)) (mapf (cdr l))))))
  84.     (else (let mapf ((l l) (rest rest))
  85.         (or (null? l)
  86.             (and (apply pred (car l) (map car rest))
  87.              (mapf (cdr l) (map cdr rest))))))))
  88.  
  89. (define (comlist:notany pred . ls) (not (apply comlist:some pred ls)))
  90.  
  91. (define (comlist:notevery pred . ls) (not (apply comlist:every pred ls)))
  92.  
  93. (define (comlist:find-if t l)
  94.   (cond ((null? l) #f)
  95.     ((t (car l)) (car l))
  96.     (else (comlist:find-if t (cdr l)))))
  97.  
  98. (define (comlist:member-if t l)
  99.   (cond ((null? l) #f)
  100.     ((t (car l)) l)
  101.     (else (comlist:member-if t (cdr l)))))
  102.  
  103. (define (comlist:remove p l)
  104.   (cond ((null? l) l)
  105.     ((eqv? p (car l)) (comlist:remove p (cdr l)))
  106.     (else (cons (car l) (comlist:remove p (cdr l))))))
  107.  
  108. (define (comlist:remove-if p l)
  109.   (cond ((null? l) l)
  110.     ((p (car l)) (comlist:remove-if p (cdr l)))
  111.     (else (cons (car l) (comlist:remove-if p (cdr l))))))
  112.  
  113. (define (comlist:remove-if-not p l)
  114.   (cond ((null? l) l)
  115.     ((p (car l)) (cons (car l) (comlist:remove-if-not p (cdr l))))
  116.     (else (comlist:remove-if-not p (cdr l)))))
  117.  
  118. (define comlist:nconc
  119.   (if (provided? 'rev2-procedures) append!
  120.       (lambda args
  121.     (cond ((null? args) '())
  122.           ((null? (cdr args)) (car args))
  123.           ((null? (car args)) (apply comlist:nconc (cdr args)))
  124.           (else
  125.            (set-cdr! (last-pair (car args))
  126.              (apply comlist:nconc (cdr args)))
  127.            (car args))))))
  128.  
  129. ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
  130. (define (comlist:nreverse rev-it)
  131. ;;; Reverse order of elements of LIST by mutating cdrs.
  132.   (cond ((null? rev-it) rev-it)
  133.     ((not (list? rev-it))
  134.      (slib:error "nreverse: Not a list in arg1" rev-it))
  135.     (else (do ((reved '() rev-it)
  136.            (rev-cdr (cdr rev-it) (cdr rev-cdr))
  137.            (rev-it rev-it rev-cdr))
  138.           ((begin (set-cdr! rev-it reved) (null? rev-cdr)) rev-it)))))
  139.  
  140. (define (comlist:butlast lst n)
  141.   (letrec ((l (- (length lst) n))
  142.        (bl (lambda (lst n)
  143.          (cond ((null? lst) lst)
  144.                ((positive? n)
  145.             (cons (car lst) (bl (cdr lst) (+ -1 n))))
  146.                (else '())))))
  147.     (bl lst (if (negative? n)
  148.         (slib:error "negative argument to butlast" n)
  149.         l))))
  150.  
  151. (define (comlist:nthcdr n lst)
  152.   (if (zero? n) lst (comlist:nthcdr (+ -1 n) (cdr lst))))
  153.  
  154. (define (comlist:last lst n)
  155.   (comlist:nthcdr (- (length lst) n) lst))
  156.  
  157. ;;;; CONDITIONALS
  158.  
  159. (define (comlist:and? . args)
  160.   (cond ((null? args) #t)
  161.     ((car args) (apply comlist:and? (cdr args)))
  162.     (else #f)))
  163.  
  164. (define (comlist:or? . args)
  165.   (cond ((null? args) #f)
  166.     ((car args) #t)
  167.     (else (apply comlist:or? (cdr args)))))
  168.  
  169. (define (comlist:identity x) x)
  170.  
  171. (define (COMLIST:LIST* x . y)    
  172.   (define (list*1 x)
  173.     (if (null? (cdr x))
  174.     (car x)
  175.     (cons (car x) (list*1 (cdr x)))))
  176.   (if (null? y)
  177.       x
  178.       (cons x (list*1 y))))
  179.  
  180. (define (COMLIST:ATOM a)
  181.   (not (pair? a)))
  182.  
  183. (define (COMLIST:DELETE obj list)
  184.   (let delete ((list list))
  185.     (cond ((null? list) '())
  186.       ((equal? obj (car list)) (delete (cdr list)))
  187.       (else
  188.        (set-cdr! list (delete (cdr list)))
  189.        list)))) 
  190.  
  191. (define (COMLIST:DELETE-IF pred list)
  192.   (let delete-if ((list list))
  193.     (cond ((null? list) '())
  194.       ((pred (car list)) (delete-if (cdr list)))
  195.       (else
  196.        (set-cdr! list (delete-if (cdr list)))
  197.        list)))) 
  198.  
  199. (define (comlist:delete-if-not pred list)
  200.   (let delete-if ((list list))
  201.     (cond ((null? list) '())
  202.       ((not (pred (car list))) (delete-if (cdr list)))
  203.       (else
  204.        (set-cdr! list (delete-if (cdr list)))
  205.        list)))) 
  206.  
  207. ;;; exports
  208.  
  209. (define make-list comlist:make-list)
  210. (define copy-list comlist:copy-list)
  211. (define adjoin comlist:adjoin)
  212. (define union comlist:union)
  213. (define intersection comlist:intersection)
  214. (define set-difference comlist:set-difference)
  215. (define position comlist:position)
  216. (define reduce-init comlist:reduce-init)
  217. (define reduce comlist:reduce) ; reduce is also in collect.scm
  218. (define some comlist:some)
  219. (define every comlist:every)
  220. (define notevery comlist:notevery)
  221. (define notany comlist:notany)
  222. (define find-if comlist:find-if)
  223. (define member-if comlist:member-if)
  224. (define remove comlist:remove)
  225. (define remove-if comlist:remove-if)
  226. (define remove-if-not comlist:remove-if-not)
  227. (define nconc comlist:nconc)
  228. (define nreverse comlist:nreverse)
  229. (define butlast comlist:butlast)
  230. (define nthcdr comlist:nthcdr)
  231. (define last comlist:last)
  232. (define and? comlist:and?)
  233. (define or? comlist:or?)
  234. (define identity comlist:identity)
  235.  
  236. (define delete-if-not comlist:delete-if-not)
  237. (define delete-if comlist:delete-if)
  238. (define delete comlist:delete)
  239. (define atom comlist:atom)
  240. (define list* comlist:list*)
  241.  
  242. (require 'rev3-procedures)
  243.